package com.wenheng.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.spring.util.BeanUtils;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.wenheng.client.OrderClient;
import com.wenheng.config.AlipayConfig;
import com.wenheng.service.PayService;
import com.wenheng.vo.entity.EasybuyOrder;
import com.wenheng.vo.entity.EasybuyProduct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Auther: 文恒
 * @Date: 2021/3/29 - 03 - 29 -9:50
 * @Description:com.wenheng.service.Impl
 * @version:1.0
 */
@Service
@Slf4j
public class PayServiceImpl implements PayService{
    @Autowired
    private OrderClient orderClient;
    @Override
    public void aliPay(HttpServletResponse response, HttpServletRequest request,String orderNum) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        //订单信息
        Map<String,Object> map = findOrderInfo(orderNum);
        System.out.println("订单数据："+map);
        List<EasybuyOrder> listOrder = JSONObject.parseArray(JSONObject.toJSONString(map.get("orderInfo")),EasybuyOrder.class);
        Map<String,Object> map1 = (Map<String, Object>) map.get("product");
        String number = JSONObject.toJSONString(map1.get("data"));
        System.out.println("订单："+number);
        EasybuyProduct product = JSONObject.parseObject(number,EasybuyProduct.class);
        EasybuyOrder order = null;
        for (EasybuyOrder easybuyOrder : listOrder) {
            order = easybuyOrder;
        }
        PrintWriter out = response.getWriter();
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE);
        //设置请求参数
        AlipayTradePagePayRequest aliPayRequest = new AlipayTradePagePayRequest();
        //aliPayRequest.setReturnUrl(AlipayConfig.return_url);
        //aliPayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //商户订单号，后台可以写一个工具类生成一个订单号，必填
//        orderNum   订单号
        //付款金额，从前台获取，必填
        String total_amount = order.getCost().toString();
        //订单名称，必填
        String subject = product.getName();
        aliPayRequest.setBizContent("{\"out_trade_no\":\"" + orderNum + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求
        String result = null;
        try {
            result = alipayClient.pageExecute(aliPayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        //输出
        out.println(result);
        out.flush();
        log.info("返回结果={}",result);
    }

    //查询订单的信息
    @Override
    public Map<String,Object> findOrderInfo(String orderId){
        return orderClient.findOrderInfo(orderId);
    }
}
